package com.bcu.cn.onlineexamsystem.adminmanage.service.impl;
import com.bcu.cn.onlineexamsystem.adminmanage.entity.AdminUser;
import com.bcu.cn.onlineexamsystem.adminmanage.mapper.AdminUserMapper;
import com.bcu.cn.onlineexamsystem.adminmanage.service.AdminUserService;
import com.bcu.cn.onlineexamsystem.exammanage.entity.Class;
import com.bcu.cn.onlineexamsystem.exammanage.mapper.ClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {


    private final AdminUserMapper userMapper;
    private final ClassMapper classMapper;

    @Autowired
    public AdminUserServiceImpl(AdminUserMapper userMapper, ClassMapper classMapper) {
        this.userMapper = userMapper;
        this.classMapper = classMapper;
    }

    @Override
    public List<AdminUser> getAllUsers() {
        return userMapper.findAllUsers();
    }

    @Override
    public AdminUser getUserById(String userId) {
        return userMapper.findUserById(userId);
    }

    @Override
    @Transactional
    public boolean createUser(AdminUser user) {
        // 不进行密码加密，直接存储明文
        return userMapper.insertUser(user) > 0;
    }

    @Override
    @Transactional
    public boolean updateUser(AdminUser user) {
        return userMapper.updateUser(user) > 0;
    }

    @Override
    @Transactional
    public boolean deleteUser(String userId) {
        return userMapper.deleteUser(userId) > 0;
    }

    @Override
    public List<AdminUser> searchUsers(String keyword, String role) {
        return userMapper.searchUsers(keyword, role);
    }

    @Override
    public int countAllUsers() {
        return userMapper.countAllUsers();
    }

    @Override
    public int countByRole(String role) {
        return userMapper.countByRole(role);
    }

    @Override
    public int countAllClasses() {
        return userMapper.countAllClasses();
    }

    @Override
    public int countUsersByDate(String dateStr) {
        return userMapper.countUsersByDate(dateStr);
    }

    @Override
    public List<Class> getAllClasses() {
        return classMapper.findAll();
    }

    @Override
    public List<Long> getTeacherCourseIds(String teacherId) {
        return userMapper.findCourseIdsByTeacher(teacherId);
    }

    @Override
    public List<Long> getTeacherClassIds(String teacherId) {
        return userMapper.findClassIdsByTeacher(teacherId);
    }

    @Override
    public void assignTeacherCourses(String teacherId, List<Long> courseIds) {
        userMapper.deleteTeacherCourses(teacherId);
        if (courseIds != null && !courseIds.isEmpty()) {
            userMapper.batchInsertTeacherCourses(teacherId, courseIds);
        }
    }

    @Override
    public void assignTeacherClasses(String teacherId, List<Long> classIds) {
        userMapper.deleteTeacherClasses(teacherId);
        if (classIds != null && !classIds.isEmpty()) {
            userMapper.batchInsertTeacherClasses(teacherId, classIds);
        }
    }

    @Override
    public List<Long> getStudentCourseIds(String studentId) {
        return userMapper.findCourseIdsByStudent(studentId);
    }

    @Override
    public void assignStudentCourses(String studentId, List<Long> courseIds) {
        userMapper.deleteStudentCourses(studentId);
        if (courseIds != null && !courseIds.isEmpty()) {
            userMapper.batchInsertStudentCourses(studentId, courseIds);
        }
    }

}
